package com.javathread.th1.sync;

import com.javathread.thread01.SleepTools;

public class SynclzAndInst {
	
	//使用类锁的线程
	private static class SynClass extends Thread {

		@Override
		public void run() {
			System.out.println("TestClass is running...");
			synClass();
		}
		
	}
	//使用对象锁的线程
	private static class InstanceSyn implements Runnable{
		private SynclzAndInst synclzAndInst;
		
		public InstanceSyn(SynclzAndInst synclzAndInst) {
			this.synclzAndInst = synclzAndInst;
		}
		
		@Override
		public void run() {
			System.out.println("TestInstance is running..." + synclzAndInst);
			synclzAndInst.instance();
		}
		
	}
	
	//锁对象
	private synchronized void instance() {
		SleepTools.second(3);
		System.out.println("synInstance is going "+this.toString());
		SleepTools.second(3);
		System.out.println("synInstance ended "+this.toString());
	}
	
	//锁对象
	private synchronized void instance2() {
		SleepTools.second(3);
		System.out.println("synInstance2 is going "+this.toString());
		SleepTools.second(3);
		System.out.println("synInstance2 ended "+this.toString());
	}
	
	private static synchronized void synClass() {
		SleepTools.second(1);
		System.out.println("synClass going...");
		SleepTools.second(1);
		System.out.println("synClass end");
	}
	
	public static void main(String[] args) {
		
		SynclzAndInst synclzAndInst = new SynclzAndInst();
		Thread t1 = new Thread(new InstanceSyn(synclzAndInst));
		t1.start();
		
		SynClass synClass = new SynClass();
		synClass.start();
		SleepTools.second(1);
	}
}
